package com.tommy.ddp
{
	import com.tommy.ddp.box.DdpBox;
	import com.tommy.ddp.definition.DDPConfig;
	import com.tommy.ddp.definition.GridSkinFactory;
	import com.tommy.ddp.grid.DdpGrid;
	import com.tommy.ddp.levelConfig.LevelXMLInfo;
	import com.tommy.ddp.manager.PipelineDataManager;
	import com.tommy.ddp.pipeline.Pipeline;
	
	import starling.display.Sprite;
	
	public class DdpContainer extends Sprite
	{
		private var xml:XML;
		
		public function DdpContainer()
		{
			super();
			//this.clipRect = new Rectangle(0,0,100,100);
			
			xml = LevelXMLInfo.getXML();
			
			createGrid();
			createBox();
			
			//创建地图上的管道信息
			PipelineDataManager.createPipelineList();
		}
		
		public function set mouseEnabled(b:Boolean):void
		{
			this.touchable = b;
		}
		
		private function createGrid():void
		{
			DDPConfig.W_NUM = xml.@x_n;
			DDPConfig.H_NUM = xml.@y_n;
			
			var gridList:XMLList = xml.grids.grid;
			for each(var i:XML in gridList)
			{
				var column:uint = uint(i.@column);
				var row:uint = uint(i.@row);
				
				var grid:DdpGrid = new DdpGrid();
				grid.column = column;
				grid.row = row;
				grid.setGridSkin(i.@type);
				addChild(grid);
				GridDataManager.regGrid(column,row,grid);
			}
			
			/*for(var i:uint = 0;i<DDPConfig.W_NUM;i++)
			{
				for(var j:uint = 0;j<DDPConfig.H_NUM;j++)
				{
					var grid:DdpGrid = new DdpGrid();
					grid.column = i;
					grid.row = j;
					grid.setGridSkin(Math.floor(Math.random()*5));
					addChild(grid);
					GridDataManager.regGrid(i,j,grid);
				}
			}*/
		}
		
		private function createBox():void
		{
			for(var i:uint = 0;i<DDPConfig.W_NUM;i++)
			{
				for(var j:uint = 0;j<DDPConfig.H_NUM;j++)
				{
					var grid:DdpGrid = GridDataManager.getGrid(i,j);
					if(grid.isAvaliable || grid.skin.type == GridSkinFactory.WIRE_NET)
					{
						var box:DdpBox = new DdpBox();
						box.column = i;
						box.row = j;
						box.setType(Math.floor(Math.random()*DDPConfig.COLOR_NUM));
						addChild(box);
						GridDataManager.regBox(i,j,box);
						if(grid.skin.type == GridSkinFactory.WIRE_NET)
						{
							addChild(grid);
						}
					}
				}
			}
			
		}
		
		
		public function destroy():void
		{
			GridDataManager.destroy();
			PipelineDataManager.dispose();
		}
	}
}